/*
 * Copyright (c) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package it.bradipao.deschartsdemo.view;

import ohos.agp.colors.RgbColor;
import ohos.agp.components.AbsButton;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.Text;
import ohos.agp.components.ToggleButton;
import ohos.agp.components.element.ShapeElement;
import ohos.app.Context;
import ohos.multimodalinput.event.TouchEvent;

import it.bradipao.deschartsdemo.ResourceTable;

/**
 * 可以设置底部横线颜色的ToggleButtom
 *
 * @since 2021-08-02
 */
public class MyToggleButton extends ToggleButton implements Text.TouchEventListener {

    private Context mContext;

    /**
     * MyToggleButton
     *
     * @param context context
     */
    public MyToggleButton(Context context) {
        this(context, null);
    }

    /**
     * MyToggleButton
     *
     * @param context context
     * @param attrSet attrSet
     */
    public MyToggleButton(Context context, AttrSet attrSet) {
        this(context, attrSet, "");
    }

    /**
     * MyToggleButton
     *
     * @param context context
     * @param attrSet attrSet
     * @param styleName styleName
     */
    public MyToggleButton(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
        this.mContext = context;
        init();
    }

    private void init() {
        updateButtonElement(false);
        setCheckedStateChangedListener(new CheckedStateChangedListener() {
            @Override
            public void onCheckedChanged(AbsButton absButton, boolean isChecked) {
                updateButtonElement(isChecked);
            }
        });
        setTouchEventListener(this);
    }


    private void updateButtonElement(boolean isChecked) {
        if (isChecked) {
            setButtonElement(new ShapeElement(mContext, ResourceTable.Graphic_toggle_checked));
        } else {
            setButtonElement(new ShapeElement(mContext, ResourceTable.Graphic_toggle_unchecked));
        }
        if (myToggleCheckedChangedListener != null) {
            myToggleCheckedChangedListener.onCheckedChanged(this, isChecked);
        }
    }

    @Override
    public boolean onTouchEvent(Component component, TouchEvent touchEvent) {
        ShapeElement shapeElement = new ShapeElement();
        shapeElement.setRgbColor(new RgbColor(211, 211, 211));
        ShapeElement shapeElement1 = new ShapeElement();
        shapeElement1.setRgbColor(new RgbColor(255, 255, 255));
        switch (touchEvent.getAction()) {
            case TouchEvent.PRIMARY_POINT_DOWN:
                setBackground(shapeElement);
                break;
            case TouchEvent.PRIMARY_POINT_UP:
                setBackground(shapeElement1);
                break;
            default:
                break;
        }
        return true;
    }

    /**
     * MyToggleCheckedChangedListener
     *
     * @since 2021-08-02
     */
    public interface MyToggleCheckedChangedListener {
        /**
         * onCheckedChanged
         *
         * @param absButton absButton
         * @param isChecked isChecked
         */
        void onCheckedChanged(AbsButton absButton, boolean isChecked);
    }

    private MyToggleCheckedChangedListener myToggleCheckedChangedListener;

    /**
     * setMyToggleCheckedChangedListener
     *
     * @param listener listener
     */
    public void setMyToggleCheckedChangedListener(MyToggleCheckedChangedListener listener) {
        this.myToggleCheckedChangedListener = listener;
    }
}

